package com.hidataplus.hadoop.tezui;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Lookup;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.impl.auth.SPNegoSchemeFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.auth.Subject;
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.Principal;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class RequestUrlUtils {
    public static Logger logger = LoggerFactory.getLogger(RequestUrlUtils.class);

    public RequestUrlUtils(){}

    private static HttpClient buildHttpClient() {

        CloseableHttpClient  httpClient = HttpClientBuilder
                .create()
                .build();
        return httpClient;
    }

    public String callRestUrlString(final String url, final String userId) throws URISyntaxException, IOException {
        HttpResponse  response = this.callRestUrl(url,userId);
        HttpEntity entity = response.getEntity();
        //使用工具类EntityUtils，从响应中取出实体表示的内容并转换成字符串
        String string = EntityUtils.toString(entity, "utf-8");
        return string;
    }

    public HttpResponse callRestUrl(final String url, final String userId) throws URISyntaxException, IOException {
        logger.info(String.format("Begin calling httpClient  %s", url));

        //创建一个uri对象
        URIBuilder uriBuilder = new URIBuilder(url);
        //塞入form参数
        uriBuilder.addParameter("user.name", userId);
        //创建httpGet远程连接实例,这里传入目标的网络地址
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        // 设置请求头信息，鉴权(没有可忽略)
//        httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
        // 设置配置请求参数(没有可忽略)
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                .setConnectionRequestTimeout(35000)// 请求超时时间
                .setSocketTimeout(60000)// 数据读取超时时间
                .build();
        // 为httpGet实例设置配置
        httpGet.setConfig(requestConfig);
        //执行请求
        HttpClient  httpClient = RequestUrlUtils.buildHttpClient();
        HttpResponse response = httpClient.execute(httpGet);
        //获取Response状态码
        int statusCode = response.getStatusLine().getStatusCode();
        logger.info(String.format("Finish calling httpClient  %s, status: %s", url,statusCode));




        return response;
    }
}
